<?php
/**
 * Created by PhpStorm.
 * Author: kaodou
 * Date: 2017/10/18
 * Time: 14:33
 */
namespace App\Http\Admin\Photo\Models;

use Framework\BaseClass\Http\Admin\Model;
use App\Eloquent\Admin\Photo as EloquentPhoto;

class Photo extends Model
{
    public function getProList()
    {
        return EloquentPhoto::all();
    }

    public function addPhoto($saveData)
    {
        $PhotoInfo = EloquentPhoto::firstOrNew(['path' => $saveData['path']]);
        if ($PhotoInfo->id) xThrow(ERR_CLIENT_USER_ALREADY_EXISTS);

        return $this->savePhoto($PhotoInfo, $saveData);
    }

    public function updatePhoto($PhotoId, $saveData)
    {
        $PhotoInfo = EloquentPhoto::find($PhotoId);
        if (!$PhotoInfo) xThrow(ERR_CLIENT_USER_NOT_EXIST);

        return $this->savePhoto($PhotoInfo, $saveData);
    }

    private function savePhoto(EloquentPhoto $PhotoInfo, array $saveData)
    {
        $PhotoInfo->fill($saveData);
        if($PhotoInfo->save()){
            return $PhotoInfo->id;
        }else{
            return false;
        }
    }
}